package com.example.springboot_mybatisplus.service.serviceImp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springboot_mybatisplus.dao.UserMapper;
import com.example.springboot_mybatisplus.entity.User;
import com.example.springboot_mybatisplus.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.handler.UserRoleAuthorizationInterceptor;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author linruhai
 * @date 2021/10/8 - 23:55
 */
@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Resource
    UserMapper userMapper;
    @Override
    public int add(User user) {
        int res=userMapper.insert(user);
        return res;
    }

    @Override
    public List<User> findAll() {
        //查询所有
        return userMapper.selectList(new LambdaQueryWrapper<>());
    }

    @Override
    public List<User> findPage(int current, int pageSize) {
        //分页查询
        Page<User> page = new Page<>(current,pageSize);
        IPage<User> userIPage = userMapper.selectPage(page, new LambdaQueryWrapper<>());
        log.info("total:{}",userIPage.getTotal());
        log.info("pages:{}",userIPage.getPages());
        return userIPage.getRecords();
    }

    @Override
    public int updateById(User user) {
        return userMapper.updateById(user);
    }

    @Override
    public int deleteById(long id){
        return userMapper.deleteById(id);
    }
}

